﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ShevaEngine.Core.Modules.Materials.Textures
{
    /// <summary>
    /// Texture tree node.
    /// </summary>
    public class TextureTreeNode
    {
        /// <summary>Nodes.</summary>
        internal SortedList<string, TextureTreeNode> Nodes;
        /// <summary>Texture state.</summary>
        internal TextureState TextureState;

        /// <summary>
        /// Constructor.
        /// </summary>
        internal TextureTreeNode()
        {            
            this.Nodes = new SortedList<string, TextureTreeNode>();
            this.TextureState = null;
        }

        /// <summary>
        /// Gets TextureState Id.
        /// </summary>
        /// <param name="textures"></param>
        /// <param name="level"></param>
        internal TextureState GetTextureState(Textures textures, int level, bool typeValue)
        {
            TextureState output = null;

            if (typeValue)
            {          
                if (this.Nodes.ContainsKey(textures.Keys[level]))
                    output = this.Nodes[textures.Keys[level]].GetTextureState(textures, level, false);
                else
                    output = this.AddNodes(textures, level, true);
            }
            else
            {
                if (this.Nodes.ContainsKey(textures.Values[level]))
                    if (level == textures.Count - 1)
                    {
                        if (this.Nodes[textures.Values[level]].TextureState != null)
                            output = this.Nodes[textures.Values[level]].TextureState;
                        else
                        {
                            this.Nodes[textures.Values[level]].TextureState = TextureManager.Instance.CreateNewTextureSet(textures);

                            output = this.Nodes[textures.Values[level]].TextureState;
                        }
                    }
                    else
                        output = this.Nodes[textures.Values[level]].GetTextureState(textures, level + 1, true);
                else
                    output = this.AddNodes(textures, level + 1, false);
            }

            return output;
        }

        /// <summary>
        /// Metho creates new nodes.
        /// </summary>
        /// <param name="textures"></param>
        /// <param name="level"></param>
        /// <param name="typeValue"></param>
        private TextureState AddNodes(Textures textures, int level, bool typeValue)
        {
            TextureState output = null;

            if (typeValue)
            {
                TextureTreeNode newNode = new TextureTreeNode();

                output = newNode.AddNodes(textures, level, false);

                this.Nodes.Add(textures.Keys[level], newNode);
            }
            else
            {
                TextureTreeNode newNode = new TextureTreeNode();

                if (level != textures.Count - 1)
                    output = newNode.AddNodes(textures, level + 1, true);
                else
                {
                    newNode.TextureState = TextureManager.Instance.CreateNewTextureSet(textures);

                    output = newNode.TextureState;
                }

                this.Nodes.Add(textures.Values[level], newNode);
            }

            return output;
        }
    }
}
